package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.dto.MenuDto;
import com.zyw.zhuangdian.pojo.in.MenuDtoIn;
import com.zyw.zhuangdian.pojo.out.MenuDtoOut;
import com.zyw.zhuangdian.service.MenuService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/menu")
@RequiredArgsConstructor
@Slf4j
public class MenuController {
    private final MenuService menuService;

    /**
     * 增加菜单项
     * @param json 父菜单:title,icon,seq,level,privilege 子菜单:title,route,belongTo,seq,level,privilege
     * @return
     */
    @RequestMapping(value = "addMenu",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addMenu(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"title","菜单名称不能为空");
        Assert.hasKeyAndValue(json,"seq","菜单顺序不能为空");
        Assert.hasKeyAndValue(json,"level","菜单层级不能为空");
        Assert.hasKeyAndValue(json,"privilege","菜单权限不能为空");

        MenuDtoIn in = BeanConvertUtil.covertBean(json, MenuDtoIn.class);
        Resp<Void> resp = menuService.addMenu(in);
        return RespUtil.OK(resp);
    }

    /**
     * 修改菜单项
     * @param json
     * @return
     */
    @RequestMapping(value = "updateMenu",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> updateMenu(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"mid","主键ID不能为空");

        MenuDtoIn in = BeanConvertUtil.covertBean(json, MenuDtoIn.class);
        Resp<Void> resp = menuService.updateMenu(in);
        return RespUtil.OK(resp);
    }

    /**
     * 删除菜单项
     * @param json
     * @return
     */
    @RequestMapping(value = "deleteMenu",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteMenu(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"mid","主键ID不能为空");

        MenuDtoIn in = BeanConvertUtil.covertBean(json, MenuDtoIn.class);
        Resp<Void> resp = menuService.deleteMenu(in);
        return RespUtil.OK(resp);
    }

    /**
     * 展示菜单项
     * @param json privilege
     * @return
     */
    @RequestMapping(value = "showMenu",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> showMenu(@RequestBody JSONObject json){
//        Assert.hasKeyAndValue(json,"privilege","菜单权限不能为空");

        MenuDtoIn in = BeanConvertUtil.covertBean(json, MenuDtoIn.class);
        Resp<List<MenuDtoOut>> resp = menuService.showMenus(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出菜单项
     * @param json privilege,level,(belongTo)
     * @return
     */
    @RequestMapping(value = "listMenu",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listMenu(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"level","菜单层级不能为空");
        Assert.hasKeyAndValue(json,"privilege","菜单权限不能为空");

        MenuDtoIn in = BeanConvertUtil.covertBean(json, MenuDtoIn.class);
        Resp<List<MenuDto>> resp = menuService.listMenus(in);
        return RespUtil.OK(resp);
    }
}
